package cate.game.activity.tendrawsoptional;

import cate.common.table.activity.tendrawsoptions.row.TenDrawsOptionsRow;
import cate.common.table.call.row.CallWeightRow;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.item.hero.StarGroupList;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.tendrawsoptional.msg.DrawRewardResp;
import cate.game.activity.tendrawsoptional.msg.TakeTenDrawRewardResp;
import cate.game.activity.tendrawsoptional.msg.TenDrawsActivityInfoResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.res.call.CallTool;
import easy.java.dev.note.NoteField;

import java.util.*;

public class TenDrawsOptionalActivity extends RoleActivitySingle {
    @NoteField("奖励")
    public Map<Integer, MixRes> drawMap;
    @NoteField("领奖记录")
    public List<Integer> record;
    @NoteField("key是位置,value是必出英雄id")
    public Map<Integer, Integer> idMap;
    @NoteField("抽取中的id")
    public int progressId;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (drawMap == null) {
            drawMap = new HashMap<>();
        }
        if (record == null) {
            record = new ArrayList<>();
        }

        if (idMap == null) {
            idMap = new HashMap<>();
        }

    }

    @Override
    public void onRoleOpen() {
        progressId = 1;
    }

    @Override
    public void onRoleEnd() {

    }

    @Override
    public void onEnter() {
        super.onEnter();
        if (record.size() == 0) {
            progressId = 1;
        } else {
            int maxRecord = record.stream().mapToInt(r->r).max().orElse(0);
            int maxId = role.getGame().table.tenDraws.tenDraws.list.stream().mapToInt(row -> row.id).max().orElse(0);
            if (maxRecord == maxId) {
                progressId = 0;
            } else {
                progressId = maxRecord + 1;
            }
        }
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public void excelCheck() {

    }

    public void noticeUpdate() {
        role.sendNow(new TenDrawsActivityInfoResp(this));
    }

    //十连
    public GameResult<TenDrawsOptionalActivity> tenDraw(int tid, int pos) {
        GameResult<TenDrawsOptionalActivity> result = new GameResult<>();
        result.data = this;
        TenDrawsOptionsRow tenDrawsRow = role.getGame().table.tenDraws.tenDraws.get(tid);
        if (tenDrawsRow == null) {
            return result.fail("读不到对应配置");
        }
//        if (tenDrawsRow.day > 0 && role.getStatus().getRegisterDay() < tenDrawsRow.day) {
//            return result.fail("今天没有奖励哦");
//        }
//        if (progressId == 0) {
//            progressId = tid;
//        } else {
        if (tid != progressId) {
            return result.fail("本轮的奖励还没抽完");
        }
//        }
        if (record.contains(tid)) {
            return result.fail("本轮抽奖已结束，作弊请注意封号风险！");
        }
        //如果已经有了四次十连，就不能再抽了
        if (drawMap.size() >= 4 && idMap.size() == 0) {
            return result.fail("抽取已结束");
        }
        if (drawMap.size() == 0 && idMap.size() == 0) {
//            idMap.addAll(tenDrawsRow.heroIdList);
            for (int i = 1; i <= tenDrawsRow.heroIdList.size(); i++) {
                idMap.put(i, tenDrawsRow.heroIdList.get(i - 1));
            }
        }
        if (!idMap.containsKey(pos)) {
            return result.fail("不存在的位置");
        }
        GameResult<MixRes> drawExe = drawExe(tenDrawsRow, pos);
        if (!drawExe.ok()) {
            return result.fail(drawExe);
        }
        drawMap.put(pos, drawExe.data);
        role.sendNow(new DrawRewardResp(drawMap));
        return result;
    }

    public GameResult<MixRes> drawExe(TenDrawsOptionsRow row, int pos) {
        GameResult<MixRes> r = new GameResult<>();
        MixRes res = new MixRes();
        r.data = res;
        List<Integer> heroIds = new ArrayList<>();
        while (heroIds.size() < 10) {
            StarGroupList.Item item = row.config.get();
            if (item == null) {
                return r.fail("配置奖池有误");
            }
            CallTool tool = new CallTool();
            CallWeightRow call = tool.doGeneralCall(role, item.groupId);
            heroIds.add(call.heroId);
        }
        int index = (int) (Math.random() * heroIds.size());
        heroIds.set(index, idMap.get(pos));
        idMap.remove(pos);
        for (Integer id :
                heroIds) {
            MixResItem item = new MixResItem(GDObj.Type.HERO, id, 1);
            res.addItem(item);
        }
        return r;
    }

    //领取
    public GameResult<TenDrawsOptionalActivity> takeReward(int tid, int pos) {
        GameResult<TenDrawsOptionalActivity> r = new GameResult<>();
        r.data = this;
        TenDrawsOptionsRow tenDrawsRow = role.getGame().table.tenDraws.tenDraws.get(tid);
        if (tenDrawsRow == null) {
            return r.fail("读不到对应配置");
        }
        if (tenDrawsRow.day > 0 && role.getStatus().getRegisterDay() < tenDrawsRow.day) {
            return r.fail("今天没有奖励哦");
        }
        if (tenDrawsRow.combatId > 0 && role.getFarm().adventure.combatIdPassed < tenDrawsRow.combatId) {
            return r.fail("请先去通关主线吧");
        }
        if (progressId != tid) {
            return r.fail("不能领取非进行中的奖励");
        }
        if (drawMap.size() == 0) {
            return r.fail("请先抽取");
        }
        MixRes res = drawMap.get(pos);
        res.add(role, true, GDOperation.TEN_DRAWS, tid);
        role.sendNow(new TakeTenDrawRewardResp(res));
        drawMap.clear();
        record.add(tid);
        idMap.clear();
        if (role.getGame().table.tenDraws.tenDraws.get(progressId + 1) != null) {
            progressId++;
        }else {
            progressId = 0;
        }
        return r;
    }
}
